package com.xy.service.mainData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xy.api.model.dto.mainData.LogisticalDTO;
import com.xy.api.model.request.mainData.LogisticalRequest;
import com.xy.api.model.response.Rows;
import com.xy.api.service.mainData.ILogisticalService;
import com.xy.auth.UserContext;
import com.xy.mapper.mainData.LogisticalMapper;
import com.xy.model.po.mainData.LogisticalPO;
import com.xy.utils.CustomBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class LogisticalServiceImpl implements ILogisticalService {

    @Autowired
    private LogisticalMapper logisticalMapper;

    @Override
    public Rows<LogisticalDTO> queryLogisticalList(LogisticalRequest request) {
        Rows<LogisticalDTO> result = new Rows<>();
        LambdaQueryWrapper<LogisticalPO> logisticalWrapper = new LambdaQueryWrapper<>();
        logisticalWrapper.eq(LogisticalPO::getIsDeleted, 0);
        if (StringUtils.hasText(request.getName())) {
            logisticalWrapper.like(LogisticalPO::getName, "%" + request.getName() + "%");
        }
        int pageSize = null != request.getPageSize() ? request.getPageSize() : 10;
        int pageNum = null != request.getPageNum() ? request.getPageNum() : 1;
        Page<LogisticalPO> LogisticalPOPage = new Page<>(pageNum, pageSize);
        LogisticalPOPage = logisticalMapper.selectPage(LogisticalPOPage, logisticalWrapper);

        result.setTotal(LogisticalPOPage.getTotal());
        result.setRows(CustomBeanUtil.copyListProperties(LogisticalPOPage.getRecords(), LogisticalDTO::new));
        return result;
    }

    @Override
    public Rows<LogisticalDTO> queryAllLogisticalList() {
        LambdaQueryWrapper<LogisticalPO> query = new LambdaQueryWrapper<>();
        query.eq(LogisticalPO::getIsDeleted, 0);
        List<LogisticalPO> logisticalPOS = logisticalMapper.selectList(query);
        if (CollectionUtils.isEmpty(logisticalPOS)) {
            return new Rows<>(new ArrayList<>(), 0);
        }
        return new Rows<>(CustomBeanUtil.copyListProperties(logisticalPOS, LogisticalDTO::new), logisticalPOS.size());
    }

    @Override
    public LogisticalDTO logisticalDetail(Long id) {
        Assert.notNull(id, "物流商ID必填");
        LogisticalDTO result = new LogisticalDTO();
        LogisticalPO logisticalPO = logisticalMapper.selectById(id);
        CustomBeanUtil.copyProperties(logisticalPO, result);
        return result;
    }

    @Override
    public LogisticalDTO editLogistical(LogisticalDTO request) {
        LogisticalPO logistical = new LogisticalPO();
        CustomBeanUtil.copyProperties(request, logistical);
        if (null != request.getId()) {
            updateLogistical(logistical);
        } else {
            addLogistical(logistical);
        }
        LogisticalDTO logisticalDTO = new LogisticalDTO();
        CustomBeanUtil.copyProperties(logistical, logisticalDTO);
        return logisticalDTO;
    }

    private void updateLogistical(LogisticalPO logistical) {
        Assert.notNull(logistical.getId(), "物流商ID必填");
        logistical.setUpdateBy(UserContext.get().getId());
        logisticalMapper.updateById(logistical);
    }

    private void addLogistical(LogisticalPO logistical) {
        logistical.setCreateBy(UserContext.get().getId());
        logisticalMapper.insert(logistical);
    }

    @Override
    public void deleteLogistical(Long id) {
        Assert.notNull(id, "物流商ID必填");

        LogisticalPO deleteRecord = new LogisticalPO();
        deleteRecord.setId(id);
        deleteRecord.setUpdateBy(UserContext.get().getId());
        deleteRecord.setIsDeleted(1);
        logisticalMapper.updateById(deleteRecord);
    }

    @Override
    public Map<Long, LogisticalDTO> getLogisticalByIds(List<Long> logisticalIds) {
        if (CollectionUtils.isEmpty(logisticalIds)) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<LogisticalPO> query = new LambdaQueryWrapper<>();
        query
                .eq(LogisticalPO::getIsDeleted, 0)
                .in(LogisticalPO::getId, logisticalIds);
        List<LogisticalPO> logisticalPOS = logisticalMapper.selectList(query);
        List<LogisticalDTO> logisticalList = CustomBeanUtil.copyListProperties(logisticalPOS, LogisticalDTO::new);
        if (CollectionUtils.isEmpty(logisticalList)) {
            return new HashMap<>();
        }
        return logisticalList.stream().collect(Collectors.toMap(LogisticalDTO::getId, e -> e));
    }
}
